/**
 * Created by oneminute on 7/18/14.
 */

var assert = require("assert");
var should = require("should");
var logger = require("../../lib/utils/logger");
var testutils = require("../testutils");
var lz = require("../../lib/utils/modules")();

describe("测试Email模块。", function () {
    var email01 = null;

    it("正常创建Email对象。", function (done) {
        email01 = new lz.Email({
            email: "test0001@lz.com",
            uid: "u_test0001"
        });
        email01.save(function (_err, _email) {
            testutils.check(done, function () {
                _email.email.should.equal("test0001@lz.com");
            });
        });
    });

    it("根据_id正常加载Email对象。", function (done) {
        var email02 = new lz.Email({_id: email01._id});
        email02.load(function(_err, _email) {
            testutils.check(done, function () {
                email02.email.should.equal(email01.email);
            });
        });
    });

    it("根据AQL正常加载Email对象。", function (done) {
        var email03 = new lz.Email();
        email03.load({method: "byProp", prop: "uid", value: email01.uid}, function(_err, _email) {
            testutils.check(done, function() {
                email03.email.should.equal(email01.email);
            });
        });
    });

    it("根据uid使用list方法正常列出Email对象。", function (done) {
        var email04 = new lz.Email({uid: email01.uid});
        email04.list({method: "byProp", prop: "uid"}, function(_err, _emails) {
            testutils.check(done, function() {
                assert.equal(_emails.length > 0, true);
                lz.Logger.info("emails: ", _emails);
            });
        });
    });

    it("使用delete方法删除Email对象。", function (done) {
        email01.delete(function(_err) {
            lz.ArangoDB.getObjectById(email01._id, function(_err, _obj) {
                testutils.check(done, function() {
                    assert.equal(_obj, null);
                });
            });
        });
    });

    it("根据uid加载不存在的对象。", function (done) {
        var email05 = new lz.Email({uid: lz.Utils.getUUID("u_")});
        email05.load({
            method: "byAQL",
            aql: lz.Config.db.arango.aqls.GET_OBJECT_BY_PROPERTY,
            params: {
                "@collection": lz.Config.db.arango.emails,
                prop: "uid",
                value: email05.uid
            }
        }, function(_err, _email) {
            testutils.check(done, function() {
                assert.equal(_err, null);
                assert.equal(_email, null);
            });
        });
    });

    after(function(done) {
        done();
    });
});
